package com.shd.edanpei.ui.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.shd.edanpei.R;
import com.shd.edanpei.base.BaseFragment;
import com.shd.edanpei.base.BaseIPresenter;
import com.shd.edanpei.base.data.RxBusCode;
import com.shd.edanpei.base.rxbus.RxBus;
import com.shd.edanpei.base.rxbus.Subscribe;
import com.shd.edanpei.common.Constant;
import com.shd.edanpei.presenter.Team1Presenter;
import com.shd.edanpei.presenter.contract.Team1Contract;
import com.shd.edanpei.ui.activity.MyTeamActivity;
import com.shd.edanpei.ui.activity.SalesmanActivity;
import com.shd.edanpei.ui.adapter.TeamAdapter;
import com.shd.edanpei.ui.adapter.TeamAdapter1;
import com.shd.edanpei.util.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by mc on 2018/11/20.
 * yang-wen-wen
 */

public class TeamFragment1 extends BaseFragment<Team1Presenter> implements Team1Contract.Team1IView {

    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.rlv_search)
    RecyclerView rlvSerach;

    private TeamAdapter1 mListAdapter;
    private TeamAdapter1 mSearchAdapter;

    private List searchList = new ArrayList();
    private int teamID;

    @Override
    protected Object setLayout() {
        return R.layout.fragment_team1;
    }

    @Override
    protected Team1Presenter bindPresenter() {
        return new Team1Presenter(this);
    }

    @Override
    protected void initView(@Nullable View view, @Nullable Bundle savedInstanceState) {
        RxBus.get().register(this);

        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mListAdapter = new TeamAdapter1(getContext());
        recyclerView.setAdapter(mListAdapter);

        rlvSerach.setLayoutManager(new LinearLayoutManager(getContext()));
        mSearchAdapter = new TeamAdapter1(getContext());
        rlvSerach.setAdapter(mSearchAdapter);

        teamID = getArguments().getInt(Constant.TEAM_ID);

        mPresenter.refresh(teamID);
    }

    @Override
    protected void initListener() {
        mListAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Bundle bundle = new Bundle();
                bundle.putInt(Constant.USER_ID, mListAdapter.getItem(position).getUserId());
                bundle.putInt(Constant.TEAM_ID, teamID);
                startActivity(SalesmanActivity.class, bundle);
            }
        });
        mListAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                mPresenter.loadMore(teamID);
            }
        }, recyclerView);


        mSearchAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Bundle bundle = new Bundle();
                bundle.putInt(Constant.USER_ID, mSearchAdapter.getItem(position).getUserId());
                bundle.putInt(Constant.TEAM_ID, teamID);
                startActivity(SalesmanActivity.class, bundle);
            }
        });
    }

    @Subscribe(code = MyTeamActivity.SEARCH)
    public void search(String str) {
        recyclerView.setVisibility(View.GONE);

        rlvSerach.setVisibility(View.VISIBLE);

        mPresenter.search(teamID, str);

    }

    /**
     * 删除搜索输入
     */
    @Subscribe(code = MyTeamActivity.NO_SEARCH)
    public void noSearch() {
        recyclerView.setVisibility(View.VISIBLE);

        rlvSerach.setVisibility(View.GONE);
        searchList.clear();
        mSearchAdapter.setNewData(searchList);

        RxBus.get().send(RxBusCode.REMOVE_ORDER_COUNT);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        RxBus.get().unRegister(this);
    }

    @Override
    public TeamAdapter1 getListAdapter() {
        return mListAdapter;
    }

    @Override
    public TeamAdapter1 getSearchAdapter() {
        return mSearchAdapter;
    }
}
